/*
 * @Author: zlei.zhou
 * @Date: 2018-03-08 17:22:39
 * @Last Modified by: zlei.zhou
 * @Last Modified time: 2018-09-12 09:09:12
 */
'use strict';

var _createClass = function () {
    function defineProperties(target, props) {
        for (var i = 0; i < props.length; i++) {
            var descriptor = props[i];
            descriptor.enumerable = descriptor.enumerable || false;
            descriptor.configurable = true;
            if ("value" in descriptor) descriptor.writable = true;
            Object.defineProperty(target, descriptor.key, descriptor);
        }
    }
    return function (Constructor, protoProps, staticProps) {
        if (protoProps) defineProperties(Constructor.prototype, protoProps);
        if (staticProps) defineProperties(Constructor, staticProps);
        return Constructor;
    };
}();

function _classCallCheck(instance, Constructor) {
    if (!(instance instanceof Constructor)) {
        throw new TypeError("Cannot call a class as a function");
    }
}

var Router = function () {
    function Router(OPTION) {
        _classCallCheck(this, Router);
        var DEFAULT = {
            reloadStyle: true, // 刷新到当前页或者刷新到首页
            cacheTime: 10000,// 页面缓存时间
            route:{}
        };
        var timeer = null;
        OPTION = $.extend(DEFAULT, OPTION);
        this.baseHash = 'home';
        this.historyHash = 'home';
        this.reloadStyle = OPTION.reloadStyle;
        this.cacheTime = OPTION.cacheTime;
        this.route = OPTION.route;
        // 路由变化方法
        this.routeChange = function () {
            try {
                var _this = this;
                window.addEventListener("hashchange", _this.debounce(function (e) {
                    var hash = window.location.hash;
                    var value = hash.replace('#', '');
                    var nowroute = _this.route[value];
                    if (nowroute) {
                        _this.changeBack(value,_this.afterEach);
                        _this.historyHash = value;
                    } else {
                        _this.setRoute(_this.historyHash);
                    }
                }));
            } catch (error) {
                console.log(error);
            }
        };
        // 页面刷新方法
        this.reload = function () {
            try {
                var _this = this;
                $(function () {
                    // 创建默认页
                    var reloadHash = void 0;
                    if (_this.reloadStyle) {
                        //刷新到当前页
                        reloadHash = window.location.hash.replace('#', '');
                    } else {
                        //刷新到首页
                        reloadHash = _this.baseHash ;
                        window.location.hash = _this.baseHash;
                    }
                    reloadHash ? reloadHash = reloadHash : reloadHash = _this.baseHash;
                    _this.changeBack(reloadHash, _this.afterReload);
                });
            } catch (error) {
                console.log(error);
            }
        };
        // 路由定向跳转方法
        this.setRoute = function (hashName) {
            var callback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function () {};
            window.location.hash = hashName;
            setTimeout(function () {
                callback();
            }, 500);
        };
        // 路由变化回调
        this.changeBack = function (value) {
            var callback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function () {};
            callback(value);
            // 动态创建iframe标签刷新页面
            var contain = document.getElementById('main');
            var route = this.route;
            $(contain).children().addClass("hide");
            if ($("iframe[src='" + route[value] + ".html']").length) {
                $("iframe[src='" + route[value] + ".html']").removeClass('hide');
                return;
            }
            var iframe = document.createElement('iframe');
            iframe.src = route[value] + ".html";
            contain.appendChild(iframe);
            // 执行缓存摧毁
            clearInterval(timeer);
            timeer = setInterval(this.clearChache, this.cacheTime);
        };
        // 清除缓存方法
        this.clearChache = function () {
            var firstIframe = $("iframe.hide:first");
            if (firstIframe.length) {
                firstIframe.remove();
            } else {
                clearInterval(timeer);
                return;
            }
        };
        // 路由改变结束方法
        this.afterEach = function(value){
            
        }
        // 页面刷新结束方法
        this.afterReload = function(value){

        }
        // 自调用初始化方法
        this.init.call(this);
    }
    //通过选择器和属性及属性值获取对应的元素
    _createClass(Router, [{
        key: 'getElementByAttr',
        value: function getElementByAttr(tag, attr, value) {
            try {
                var aElements = document.querySelectorAll(tag);
                var obj = {};
                for (var i = 0; i < aElements.length; i++) {
                    if (aElements[i].getAttribute(attr) == value) {
                        obj.aEle = aElements[i];
                        obj.index = i;
                    }
                }
                return obj;
            } catch (error) {
                console.log(error);
            }
        }
        //事件防抖
    }, {
        key: 'debounce',
        value: function debounce(fn, delay) {
            var delay = delay || 100;
            var timer;
            return function () {
                var th = this;
                var args = arguments;
                if (timer) {
                    clearTimeout(timer);
                }
                timer = setTimeout(function () {
                    timer = null;
                    fn.apply(th, args);
                }, delay);
            };
        }
        // 提示信息

    }, {
        key: 'alertMsg',
        value: function alertMsg(length) {
            if ($("iframe").length >= length) {
                layer.msg("窗口不能超过" + length + "个");
                return false;
            }
            return true;
        }
        //初始化方法
    }, {
        key: 'init',
        value: function init() {
            this.reload() //刷新页面
            this.routeChange(); //改变锚点
        }
    }]);

    return Router;
}();